package com.jxdinfo.hussar.cloud.module.example.lift.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.cloud.module.example.lift.dao.LiftDao;
import com.jxdinfo.hussar.cloud.module.example.lift.dao.LiftHistoryCountDao;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftHistoryCountDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftHistoryCountLineDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftHistoryCountSearchDto;
import com.jxdinfo.hussar.cloud.module.example.lift.model.Lift;
import com.jxdinfo.hussar.cloud.module.example.lift.model.LiftHistoryCount;
import com.jxdinfo.hussar.cloud.module.example.lift.service.LiftHistoryCountService;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 升降机历史统计(LiftHistoryCount)表服务实现类
 *
 * @author makejava
 * @since 2023-10-23 18:26:21
 */
@Service("liftHistoryCountService")
public class LiftHistoryCountServiceImpl extends ServiceImpl<LiftHistoryCountDao, LiftHistoryCount> implements LiftHistoryCountService {
    @Resource
    private LiftHistoryCountDao liftHistoryCountDao;
    @Resource
    private LiftDao liftDao;
    /**
     * 升降机功效分析折线图表
     * @param dto
     * @return
     */
    @Override
    public List<LiftHistoryCountLineDto> findByLine(LiftHistoryCountSearchDto dto) {
        LambdaQueryWrapper<Lift> liftWrapper = new LambdaQueryWrapper<>();
        liftWrapper.eq(ObjectUtils.isNotEmpty(dto.getTyProjectCode()),Lift::getTyProjectCode,dto.getTyProjectCode())
                .eq(Lift::getTyDeleteStatus,0)
                .in(ObjectUtils.isNotEmpty(dto.getLiftId()),Lift::getId, dto.getLiftId().split(","));
        List<Lift> lifts = liftDao.selectList(liftWrapper);

        //封装查询条件
        LambdaQueryWrapper<LiftHistoryCount> historyWrapper = new LambdaQueryWrapper<>();
        historyWrapper.eq(StrUtil.isNotBlank(dto.getTyProjectCode()),LiftHistoryCount::getTyProjectCode,dto.getTyProjectCode())
                        .ge(ObjectUtils.isNotEmpty(dto.getCountDateStart()),LiftHistoryCount::getCountDate,dto.getCountDateStart())
                        .lt(ObjectUtils.isNotEmpty(dto.getCountDateEnd()),LiftHistoryCount::getCountDate,dto.getCountDateEnd())
                .in(LiftHistoryCount::getLiftId,lifts.stream().map(Lift::getId).collect(Collectors.toList()))
                .orderByDesc(LiftHistoryCount::getCountDate);

        List<LiftHistoryCount> list = liftHistoryCountDao.selectList(historyWrapper);

        List<LiftHistoryCountDto> historyList = BeanUtil.copyToList(list, LiftHistoryCountDto.class);

        Map<String, List<LiftHistoryCountDto>> map = historyList.stream().collect(Collectors.groupingBy(data -> DateUtils.formatDate(data.getCountDate())));

        List<LiftHistoryCountLineDto> dataList = new ArrayList<>();

//        List<String> dateList = DateHandleUtil.completionDateList(dto.getCountDateStart(), dto.getCountDateEnd(), map.keySet());
//
//        for(String date : dateList) {
//            LiftHistoryCountLineDto line = new LiftHistoryCountLineDto();
//            line.setCountDate(date);
//
//            List<LiftHistoryCountDto> lineData = map.get(date);
//            if(lineData == null) {
//                lineData = new ArrayList<>();
//            }
//
//            Set<String> liftHistoryCountSet = lineData.stream().map(LiftHistoryCountDto::getLiftId).collect(Collectors.toSet());
//            for (Lift lift : lifts) {
//                if (!liftHistoryCountSet.contains(lift.getId())) {
//                    LiftHistoryCountDto liftHistoryCountDto = new LiftHistoryCountDto();
//                    liftHistoryCountDto.setLiftId(lift.getId());
//                    liftHistoryCountDto.setLiftName(lift.getLiftName());
//                    liftHistoryCountDto.setWorkNum(0);
//                    liftHistoryCountDto.setWorkWeight(BigDecimal.ZERO);
//                    lineData.add(liftHistoryCountDto);
//                }
//            }
//            line.setDataList(lineData);
//            dataList.add(line);
//        }

        return dataList;
    }

    @Override
    public Page<LiftHistoryCount> findPage(LiftHistoryCountSearchDto dto) {
        LambdaQueryWrapper<Lift> liftWrapper = new LambdaQueryWrapper<>();
        liftWrapper.eq(ObjectUtils.isNotEmpty(dto.getTyProjectCode()),Lift::getTyProjectCode,dto.getTyProjectCode())
                .eq(Lift::getTyDeleteStatus,0)
                .in(ObjectUtils.isNotEmpty(dto.getLiftId()),Lift::getId, dto.getLiftId().split(","));
        List<Lift> lifts = liftDao.selectList(liftWrapper);

        LambdaQueryWrapper<LiftHistoryCount> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ObjectUtils.isNotEmpty(dto.getTyProjectCode()),LiftHistoryCount::getTyProjectCode,dto.getTyProjectCode())
                .ge(ObjectUtils.isNotEmpty(dto.getCountDateStart()),LiftHistoryCount::getCountDate,dto.getCountDateStart())
                .le(ObjectUtils.isNotEmpty(dto.getCountDateEnd()),LiftHistoryCount::getCountDate,dto.getCountDateEnd())
                .in(LiftHistoryCount::getLiftId,lifts.stream().map(Lift::getId).collect(Collectors.toList()))
                        .orderByDesc(LiftHistoryCount::getCountDate);

        Page<LiftHistoryCount> page = new Page(dto.getPage(),dto.getRows());
        Page<LiftHistoryCount> mapsPage = liftHistoryCountDao.selectPage(page, queryWrapper);
        return mapsPage;
    }
}

